$(function () {

    $.ajax({
        url:"/departs",
        type:"GET",
        async:false,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        success:function (data) {
            if (data.code==="success"){
                for (i=0;i<data.data.length;i++){
                    $("#departList").append('<li onclick="select('+data.data[i].bmdm+')">'+data.data[i].bmmc+'</li>')
                }
            }else{
                alert("获取部门列表失败！");
            }
        }
    })

    $.ajax({
        url:"/users",
        type:"GET",
        async:false,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data:{},
        success:function (data) {
            if (data.code==="success"){
                flushHtml(data.data);
            }else {
                alert("查询失败!");
            }
        }
    })


    //查询按钮绑定事件
    $('#selectBtn').bind('click',function () {
        var userID = trim($("#userID").val());
        $.ajax({
            url:"/yhxmUsers",
            type:"POST",
            async:false,
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data:{
                yhxm:userID
            },
            success:function (data) {
                if (data.code==="success"){
                    flushHtml(data.data);
                }else {
                    alert("查询失败!");
                }
            }
        })
    });
    // 新增按钮绑定事件
    $('#insertBtn').bind('click',function () {
		var height = 400;
		var width = 600;
		var top = (window.screen.availHeight - 30 - height) / 2;
		var left = (window.screen.availWidth - 10 - width) / 2;
		window.open("/add", "新增用户", 'height=' + height + ',,innerHeight=' + height +
			',width=' + width + ',innerWidth=' + width + ',top=' + top + ',left=' + left +
			',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
    });

    $("#loginout").bind('click',function () {
        loginout();
    })
});
// 对参数编码
function encodeStr(val) {
    return encodeURIComponent(encodeURIComponent(trim(val)));
}
// 去掉空格
function trim(value) {
    if (value){
        value = value.replace(/^\s*|\s*$/g, "");
    }
    if (!value){
        return "";
    }
    else{
        return value;
    }
}
//点击侧边栏部门列表时触发的查询方法
function select(bmdm) {
    $.ajax({
        url:"/bmdmUsers",
        type:"POST",
        async:false,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data:{
            bmdm:bmdm
        },
        success:function (data) {
            if (data.code==="success"){
                flushHtml(data.data);
            }else {
                alert("查询失败!");
            }
        }
    })
}
//刷新中间显示窗口
function flushHtml(userVos) {
    var index = 1;
    var table = $("#userList");
    table.empty();
    table.append(' <tr id="head">\n' +
        '                                <th width="180px">排序</th>\n' +
        '                                <th width="180px">姓名</th>\n' +
        '                                <th width="180px">账号</th>\n' +
        '                                <th width="180px">用户部门</th>\n' +
        '                                <th width="180px">性别</th>\n' +
        '                                <th width="180px">操作</th>\n' +
        '                            </tr>');
    for (i=0;i<userVos.length;i++){
        var html = " <tr>\n" +
            "                                        <td>"+index+++"</td>\n" +
            "                                        <td>"+userVos[i].yhxm+"</td>\n" +
            "                                        <td>"+userVos[i].yhid+"</td>\n" +
            "                                        <td>"+userVos[i].bmmc+"</td>\n" +
            "                                        <td>"+userVos[i].yhxb+"</td>\n" +
            "                                        <td>\n" +
            "                                            <button onclick=\"deleteUser(this.value)\" class=\"deleteUser\" value=\""+userVos[i].yhdm+"\" >删除</button>\n" +
            "                                            <button onclick=\"editUser(this.value)\" value=\""+userVos[i].yhdm+"\">修改</button>\n" +
            "                                            <button onclick=\"scanUser(this.value)\" value=\""+userVos[i].yhdm+"\">查看</button>\n" +
            "                                        </td>\n" +
            "                                    </tr>\n";
        table.append(html);
    }

}
//删除用户方法
function deleteUser(yhdm) {
    if (confirm("确认删除") == true) {
        $.ajax({
            url:"deleteOne",
            type:"POST",
            async:false,
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            data:{
                yhdm:yhdm
            },
            success:function (data) {
                if (data.code==="success"){
                    flushHtml(data.data);
                }else {
                    alert("查询失败!");
                }
            }
        })
    }else{
        return false;
    }

}
//打开编辑用户界面的方法
function editUser(yhdm) {
    var height = 400;
    var width = 600;
    var top = (window.screen.availHeight - 30 - height) / 2;
    var left = (window.screen.availWidth - 10 - width) / 2;
    window.open("/add?yhdm="+yhdm+"&method=edit", "新增用户", 'height=' + height + ',,innerHeight=' + height +
        ',width=' + width + ',innerWidth=' + width + ',top=' + top + ',left=' + left +
        ',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
}
//打开查看用户信息界面的方法
function scanUser(yhdm) {
    var height = 400;
    var width = 600;
    var top = (window.screen.availHeight - 30 - height) / 2;
    var left = (window.screen.availWidth - 10 - width) / 2;
    window.open("/add?yhdm="+yhdm+"&method=scan", "新增用户", 'height=' + height + ',,innerHeight=' + height +
        ',width=' + width + ',innerWidth=' + width + ',top=' + top + ',left=' + left +
        ',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
}
//注销用户登录的方法
function loginout() {
    $.ajax({
        url:"logout",
        type:"GET",
        async:false,
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        data:{},
        success:function () {
            window.location.href="/login";
        }
    })
}